/**
 * 
 */
package com.skivingcloud.file.store;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.skivingcloud.file.FilePathUtil;
import com.skivingcloud.file.entity.SysFile;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


/**
 * 文件存储方案的实现，本地文件系统
 * @author hushouquan
 *
 */
@Service("fsFileStorageDao")
public class FsFileStorageDaoImpl implements FileStorageDao {

	@Value("${skivingcloud.file.location:D:\\tmp\\}")
	private String fileLocation;
	
	/**
	 * 保存文件的方法
	 * @param fileManagement
	 * @param fileInputStream 
	 * @throws Exception 
	 */
	@Override
	public void saveFile(SysFile fileManagement, InputStream fileInputStream) throws Exception {
		String newPath = FilePathUtil.getFileFolder(fileLocation);
		File file = new File(newPath + fileManagement.getId() + "@" + fileManagement.getFileName());
		file.createNewFile();
		OutputStream  outputStream = new FileOutputStream(file);
		try {
			byte[] buffer = new byte[fileInputStream.available()];
			fileInputStream.read(buffer);
			outputStream.write(buffer);
			fileManagement.setFilePath(newPath + fileManagement.getId() + "@" + fileManagement.getFileName());
		} catch (IOException e) {
			throw e;
		} finally {
			if(fileInputStream != null) {
				fileInputStream.close();
			}
			if(outputStream != null) {
				outputStream.close();
			}
		}
	}

	/**
	 * 读取文件的方法
	 * @param fileManagement
	 * @return
	 * @throws Exception 
	 */
	@Override
	public InputStream getFile(SysFile fileManagement) throws Exception {
		return new FileInputStream(fileManagement.getFilePath());
	}

	/**
	 * 作废或删除文件，实际上是把文件移动到“作废”或“已删除”文件夹下
	 * @param fileManagement
	 * @param fileState
	 * @throws Exception 
	 */
	@Override
	public void moveFilePath(SysFile fileManagement, String fileState) throws Exception {
		FileInputStream fis = null;
		FileOutputStream fos = null;
		String fileRoot = fileLocation + (fileState.equals("2") ? "作废" : "已删除" + File.separator);
		try {
			fis = new FileInputStream(fileManagement.getFilePath());//创建输入流对象
			File fileOld = new File(fileManagement.getFilePath());
			String newPath = "";
			if(System.getProperty("os.name").toLowerCase().startsWith("win")) {
				newPath = fileManagement.getFilePath().replaceFirst(fileLocation.replaceAll("\\\\", "\\\\\\\\"), fileRoot.replaceAll("\\\\", "\\\\\\\\"));
			}else {
				newPath = fileManagement.getFilePath().replaceFirst(fileLocation, fileRoot);
			}
			File dir = new File(newPath.replaceAll(fileManagement.getId() + "@" + fileManagement.getFileName(), ""));
			if (!dir.exists()) {
				// 判断目录是否存在 
			    dir.mkdirs();  
			}
			File file = new File(newPath);
			if (!file.exists()) {
				// 判断目录是否存在 
				file.createNewFile();  
			}
			fos = new FileOutputStream(newPath); //创建输出流对象        
			byte[] buffer = new byte[fis.available()];
			fis.read(buffer);
			fos.write(buffer);
			fileManagement.setFilePath(newPath);
			fileOld.deleteOnExit();
		} catch (Exception e) {
			throw e;
		} finally {
			if(fis != null) {
				fis.close();
			}
			if(fos != null) {
				fos.close();
			}
		}
	}

}
